<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class School extends Console_Controller{
	
	public function __construct(){
		parent::__construct();
		$this->load->model('school_model');
	}
	
	public function index(){
		$gets = $this->input->get();
		$where = [];
		if($gets['keyword']){
			$where['title like'] = "%$gets[keyword]%";
		}
		$assign = [
			'gets' => $gets
		];
		$total = $this->school_model->get_total($where);
		if($total > 0){
			$this->load->model('admin_model');
			$userlist = $this->admin_model->get_result([
				'groupid' => 59,
				'school_id >' => 0
			]);
			$userlist = array_column($userlist, 'username', 'school_id');
			$this->load->library('pagination', [
				'total_num' => $total
			]);
			$assign['show_page'] = $this->pagination->create();
			$list = $this->school_model->get_result([
				'where' => $where,
				'start' => $this->pagination->limit_start,
				'limit' => $this->pagination->page_size
			]);
			foreach($list as &$row){
				$row['admin'] = $userlist[$row['id']];
			}
			$assign['list'] = $list;
		}
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('school_list.htm');
	}
	
	public function create(){
		$this->render('create');
	}
	
	public function edit(){
		$this->render('edit');
	}
	
	private function render($action){
		$assign = [
			'action' => $action,
			'gets' => $this->input->get()
		];
		switch($action){
			case 'create':
				$assign['title'] = '新增学校';
				break;
			case 'edit':
				$info = $this->school_model->get_row([
					'where' => [
						'id' => $this->input->get('id')
					]
				]);
				$assign['title'] = '编辑学校';
				break;
			default:
				$this->show_message();
		}
		$assign['info'] = $info;
		$this->ci_smarty->assign($assign);
		$this->ci_smarty->display('school_set.htm');
	}
	
	public function submit(){
		$posts = $this->input->post();
		if(! $posts){
			$this->output_json();
		}
		$data = array(
			'title' => $posts['title'],
			'displayorder' => $posts['displayorder']
		);
		switch($posts['action']){
			case 'create':
				break;
			case 'edit':
				break;
			default:
				$this->output_json();
		}
		$query = $this->school_model->set($data, $posts['id']);
		if($query){
			$this->output_json('提交成功', 0);
		}
		$this->output_json('提交失败');
	}
	
	public function delete(){
		$this->action('delete');
	}
	
	private function action($method){
		/* $actids = $this->input->post('actids');
		if(! $actids){
			$this->output_json();
		}
		switch($method){
			case 'delete':
				$query = $this->school_model->del([
					'where_in' => [
						'id' => $actids
					]
				]);
				break;
			default:
				$this->output_json();
		}
		if($query){
			$this->output_json('操作成功', 0);
		}
		$this->output_json('操作失败'); */
	}
	
	// 绑定管理员
	public function bind_admin(){
		$posts = $this->input->post();
		if(!$posts['username'] || !$posts['actid']){
			$this->output_json();
		}
		$this->load->model('admin_model');
		$user = $this->admin_model->get_row([
			'username' => $posts['username'],
			'groupid' => 59
		]);
		if(!$user){
			$this->output_json('用户不存在');
		}
		$query = $this->admin_model->set([
			'school_id' => $posts['actid']
		], $user['id']);
		if($query){
			$this->output_json('绑定成功', 0);
		}
		$this->output_json('绑定失败');
	}
	
}
